﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class ImgTxtObjectBase : MonoBehaviour
{
    /// <summary>
    /// 进池子
    /// </summary>
    public virtual void InPool()
    {
        transform.SetParent(EmojiTest.ObjPoolTsf);
    }

    /// <summary>
    /// 出池子
    /// </summary>
    public virtual void OutPool()
    {
        
    }

    //是否使用中
    public bool IsUsing
    {
        get;set;
    }
}

//池子管理器
public class ImgTxtObjectPoolMgr<T> where T : ImgTxtObjectBase
{
    private static List<T> m_EmojiObjectPooledList = new List<T>();

    public static T Spawn(ImgTxtType type)
    {
        T t = default(T);

        string path = null;
        if (m_EmojiObjectPooledList.Count > 0)
        {
            t = m_EmojiObjectPooledList[0];
            m_EmojiObjectPooledList.RemoveAt(0);

        }
        else
        {
            switch (type)
            {
                case ImgTxtType.staticEmoji:
                    path = "Assets/7.EmojiTextDemo/Prefab/ImageObject.prefab";
                    break;
                case ImgTxtType.hyperlink:
                    path = "Assets/7.EmojiTextDemo/Prefab/HyperlinkObject.prefab";
                    break;
            }

            if (path != null)
            {
                GameObject go = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(path));
                if (go != null)
                    t = go.GetComponent<T>();
            }
        }

        if (t != null)
        {
            t.IsUsing = true;
            t.OutPool();
        }

        return t;
    }

    public static void Recycle(T classObject)
    {
        if (classObject == null || !classObject.IsUsing)
            return;

        classObject.IsUsing = false;
        classObject.InPool();
        m_EmojiObjectPooledList.Add(classObject);
    }
}

/// <summary>
/// 图文混排类型
/// </summary>
public enum ImgTxtType
{
    staticEmoji,//静态图标
    hyperlink,//超链接
}
